﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using RibbonEditor.Forms.Rules;
using RibbonEditor.Rules;
using RibbonEditor.AppCode;

namespace RibbonEditor
{
    public class DisplayRules: IXmlBuildable, IChildDeletable
    {
        private bool isFromCmdDef = false;

        public List<DisplayRule> Rules;

        public DisplayRules(bool isFromCommandDefinition)
        {
            try
            {
                this.Rules = new List<DisplayRule>();
                this.isFromCmdDef = isFromCommandDefinition;
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        
        public DisplayRules(XmlNode dRuleXmlNode, bool isFromCommandDefinition) : this(isFromCommandDefinition)
        {
            try
            {
                foreach (XmlNode node in dRuleXmlNode.ChildNodes)
                {
                    DisplayRule dRule = new DisplayRule(node, !isFromCommandDefinition);
                    this.Rules.Add(dRule);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void AddNode(XmlNode ruleDefinitionsXmlNode)
        {
            try
            {
                XmlNode displayRulesNode = XmlHelper.AddNode(ruleDefinitionsXmlNode, "DisplayRules");

                foreach (DisplayRule dRule in this.Rules)
                {
                    dRule.AddNode(displayRulesNode);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        internal void BuildTreeViewElement(TreeNode parentNode)
        {
            try
            {
                TreeNode displayRulesNode = new TreeNode("DisplayRules");
                displayRulesNode.Tag = this;
                displayRulesNode.ContextMenuStrip = this.BuildRulesMenuStrip(displayRulesNode);

                foreach (DisplayRule rule in this.Rules)
                {
                    rule.BuildTreeViewElement(displayRulesNode);
                }

                parentNode.Nodes.Add(displayRulesNode);
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private ContextMenuStrip BuildRulesMenuStrip(TreeNode relatedNode)
        {
            ContextMenuStrip cms = new ContextMenuStrip();
            cms.Tag = relatedNode;
            
            ToolStripMenuItem tsmi = new ToolStripMenuItem("Add Rule");
            tsmi.Click += new EventHandler(tsmi_Click);
            cms.Items.Add(tsmi);

            return cms;
        }

        void tsmi_Click(object sender, EventArgs e)
        {
            try
            {
                TreeNode selectedNode = (TreeNode)((ToolStripItem)sender).GetCurrentParent().Tag;

                if (this.isFromCmdDef)
                {
                    DisplayRule dRule = new DisplayRule(false);
                    dRule.BuildTreeViewElement(selectedNode);

                    this.Rules.Add(dRule);
                }
                else
                {
                    RuleSelectionDialog rsDialog = new RuleSelectionDialog(RuleSelectionDialog.RuleType.DisplayRule);
                    rsDialog.StartPosition = FormStartPosition.CenterParent;

                    if (rsDialog.ShowDialog() == DialogResult.OK)
                    {
                        IDisplayRule selectedRule = (IDisplayRule)rsDialog.SelectedRule;

                        // Construction de l'élément parent à la règle sélectionnée (DisplayRule)
                        DisplayRule dRule = new DisplayRule(true);
                        dRule.Rules.Add(selectedRule);

                        this.Rules.Add(dRule);
                        dRule.BuildTreeViewElement(selectedNode);
                    }
                }

                selectedNode.Expand();
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #region IChildDeletable Members

        public void DeleteChild(object o)
        {
            try
            {
                if (o is DisplayRule)
                {
                    this.Rules.Remove((DisplayRule)o);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion
    }
}
